#include <sys/system.h>
#include <pages/pagectl.h>
#include <pages/page.h>
#include <gal/ptgui.h>
#include <gal/surface.h>
#include <widgets/widget.h>
#include <ial/input.h>
#include <gal/region.h>

/*	Event Process Table	*/
static	void ptgui_page_entry_handler(ptgui_page_entry_type_t Requese)
{
	if (PTGUI_PAGE_PREPARE_FST == Requese) {
	
	}
	else if (PTGUI_PAGE_PREPARE_ALL == Requese) {
	}
	else if (PTGUI_PAGE_TERMINATE_ALL == Requese) {
		//Do Nothing
	}
	else if (PTGUI_PAGE_TERMINATE_LST == Requese) {
	}
	else {
		//Do Nothing
	}
}

static	ptgui_page_control_t nvdProcPsvUpdateEndCancel(pt_event_t *EventCode)
{
	return	PTGUI_PAGE_TRAN_RETURN_REQUEST(FP_PAGE_BOOT);
}

static	ptgui_page_sw_state_t nusGetViewPsvUpdateEndCancel(ptgui_page_switch_view_t *Switch)
{
	return	PAGE_SW_STAT_VISIBLE;
}

static void ptgui_page_render_handler(const struct ptgui_page_cell *page_cell)
{
	ptgui_surface_t *picture;
	ptgui_rect_t dest, update;
	ptgui_surface_t *screen = ptgui_surface_get_video_surface();
	char *bmpfile = "BG.bmp";		/* Sample image */

	picture = ptgui_surface_create_from_file(bmpfile);
	if ( picture == PT_NULL ) {
		pt_kprintf("Couldn't load %s: %s\n", bmpfile, ptgui_surface_get_error());
	}

	dest.x1 = 0;
	dest.y1 = 0;
	dest.x2 = dest.x1 + ptgui_surface_get_width(picture);
	dest.y2 = dest.y1 + ptgui_surface_get_height(picture);

	update = dest;
	if ( ptgui_surface_blit(picture, PT_NULL, screen, &update) < 0 ) {
		pt_kprintf("Blit failed: %s\n", ptgui_surface_get_error());
	}
	
	/* Free the picture and return */
	ptgui_surface_free(picture);
}

static void ptgui_page_frame_handler(const struct ptgui_page_cell *page_cell, pt_uint32_t delta)
{

}

static ptgui_page_control_t ptgui_page_key_handler(pt_event_t* message)
{
	ptgui_page_key_event_break();
	return PTGUI_PAGE_TRAN_CALL_REQUEST(FP_PAGE_DRAW);
}
/*	Opening Cancel Key Definition	*/
PTGUI_PAGE_DEF_SW_VIEW(TswPsvUpdateEndCancel, nvdProcPsvUpdateEndCancel, nusGetViewPsvUpdateEndCancel, S_FK_RETURN_G, PAGE_POSITION_ID_1, 0,
					   (KEY_ON_DO));

/*	Static Key Table	*/
PTGUI_PAGE_BEGIN_REF_SW_VIEW_TABLE 
PTGUI_PAGE_REF_SW_VIEW(TswPsvUpdateEndCancel),
PTGUI_PAGE_END_REF_SW_VIEW_TABLE(FP_PAGE_BOOT);

PTGUI_PAGE_BEGIN_DEF_EVENT_ACCEPT 
PTGUI_PAGE_DEF_EVENT_ACCEPT(EV_PAGECTL_KEY_ON, ptgui_page_key_handler),
PTGUI_PAGE_END_DEF_EVENT_ACCEPT;

PTGUI_PAGE_DEF_PAGECELL(FP_PAGE_BOOT, PAGE_FLAG_DEFAULT);